<template>
  <div class="home">
    <div class="search">搜索</div>
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="item in swipers" :key="item.id">
        <van-image :src="item.img" />
      </van-swipe-item>
    </van-swipe>

    <van-nav-bar
      class="hot"
      left-text="正在热映"
      right-text="按钮"
      @click-right="onClickRight(0)"
    >
      <template #right>
        <span>全部{{ hotMovieList.length }}部</span>
        <van-icon name="arrow" />
      </template>
    </van-nav-bar>
    <!-- 正在热映列表 -->
    <van-grid :column-num="3" :border="false">
      <van-grid-item
        v-for="movie in hotMovieList"
        :key="movie.movie_id"
        @click="
          $router.push({ name: 'detail', params: { id: movie.movie_id } })
        "
      >
        <!-- 因为图片全部保存在后台的服务器上，需要带上后台服务器的地址才能访问 -->
        <van-image :src="BACKEND_URL + movie.poster" />

        <div class="buy">
          <h3 class="movie-title">
            {{ movie.name }} <span>{{ movie.score || 0 }}</span>
          </h3>
          购票
        </div>
      </van-grid-item>
    </van-grid>

    <van-nav-bar
      left-text="即将上映"
      right-text="按钮"
      @click-right="onClickRight(1)"
    >
      <template #right>
        <span>全部{{ comingMovieList.length }}部</span>
        <van-icon name="arrow" />
      </template>
    </van-nav-bar>

    <!-- 即将上映列表 -->
    <van-grid :column-num="3">
      <van-grid-item v-for="movie in comingMovieList" :key="movie.movie_id">
        <!-- 因为图片全部保存在后台的服务器上，需要带上后台服务器的地址才能访问 -->
        <van-image :src="BACKEND_URL + movie.poster" />

        <div class="buy">
          <h3 class="movie-title">
            {{ movie.name }} <span>{{ movie.score || 0 }}</span>
          </h3>
          购票
        </div>
      </van-grid-item>
    </van-grid>

    <van-tabs v-model="active">
      <van-tab title="标签 1"> <span @click="jump(1)">内容跳转</span> 1</van-tab>
      <van-tab title="标签 2"> <span @click="jump(2)">内容跳转</span> 2</van-tab>
      <van-tab title="标签 3"> <span @click="jump(3)">内容跳转</span> 3</van-tab>
      <van-tab title="标签 4"> <span @click="jump(4)">内容跳转</span> 4</van-tab>
    </van-tabs>
  </div>
</template>

<script>
import { BACKEND_URL } from '@/config'
import { getMovieList } from '@/api/movie'
export default {
  name: 'HomeView',
  data () {
    return {
      active: 0,
      swipers: [
        {
          id: 1,
          img: require('../assets/swiper/1.jpg')
        },
        {
          id: 2,
          img: require('../assets/swiper/2.jpg')
        },
        {
          id: 3,
          img: require('../assets/swiper/3.jpg')
        },
        {
          id: 4,
          img: require('../assets/swiper/4.jpg')
        }
      ],
      movieList: [],
      BACKEND_URL: BACKEND_URL
    }
  },
  computed: {
    hotMovieList () {
      // 过滤电影，过滤出电影的上映时间比当前时间靠前的电影 并且只要索引小于等于5的
      return this.movieList.filter(
        (item, index) => +new Date(item.public_date) < Date.now()
      )
    },
    comingMovieList () {
      // 当前时间戳
      const current = Date.now()
      // 过滤电影，过滤出电影的上映时间比当前时间靠后的电影
      return this.movieList.filter((item) => {
        const time = +new Date(item.public_date)
        return time > current
      })
    }
  },
  methods: {
    onClickRight (id) {
      this.$router.push({ name: 'movie', query: { id } })
    },
    jump (id) {
      this.$router.push({ name: 'detail', params: { id: id } })
    }
  },
  async created () {
    const res = await getMovieList()
    this.movieList = res.data.data
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition((res) => {
        console.log(res) // 这里会返回经纬度，然后还要通过经纬度转换地区名称
      })
    }
  }
}
</script>

<style lang="less" scoped>
.home {
  .search {
    position: fixed;
    height: 40px;
    width: 100%;
    z-index: 9999;
    display: flex;
    background-color: rgba(0, 0, 0, 0.5);
    color: white;
  }
  // 改变组件库的样式可能不会成功，因为这里加了scoped。要想让样式生效，使用样式穿透 /deep/ ::v-deep
  ::v-deep .hot .van-nav-bar__text {
    color: red;
  }
  /deep/ .van-nav-bar .van-icon {
    color: black;
  }
  /deep/ .van-grid-item {
    width: 33.3333%;
  }
  .movie-title {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    width: 80%;
    font-size: 10px;
    text-align: left;
    position: absolute;
    top: -20px;
    width: 100%;
    height: 20px;
    line-height: 20px;
    background-color: rgba(0, 0, 0, 0.3);
    color: white;
    text-indent: 5px;
    span {
      float: right;
      margin-right: 5px;
    }
  }
  .buy {
    color: white;
    font-size: 10px;
    background-color: #dd2727;
    text-align: center;
    height: 30px;
    line-height: 30px;
    width: 100%;
    display: block;
    position: relative;
  }
}
</style>
